/*
* Copyright (c) 2008 by Windward Studios, Inc. All rights reserved.
*
* This software is the confidential and proprietary information of
* Windward Studios ("Confidential Information").  You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Windward Studios, Inc.
*/

using System;
using System.Drawing;
using System.Windows.Forms;
using Kailua.net.windward.utils;
using net.windward.utils;

namespace Kailua.net.windward.controls
{
	/// <summary>
	/// A TreeNode in a LinkTreeView. This is similiar to a normal tree except each node can have multiple links in it.
	/// </summary>
	public class LinkTreeNode : TreeNode
	{
		private readonly LinkTreeNodeChunkCollection collChunks;

		private object deleteChunk;
		private bool deleteVisible = true;
		private bool deleteEnabled = true;
		private string text;

		internal Rectangle[] under;
		internal Rectangle deleteRect;

		/// <summary>
		/// Create the object.
		/// </summary>
		/// <param name="text">The text for the node.</param>
		/// <param name="imageIndex">The image used if the node is not selected.</param>
		/// <param name="selectedImageIndex">The image used if the node is selected.</param>
		public LinkTreeNode(string text, int imageIndex, int selectedImageIndex) : base(text, imageIndex, selectedImageIndex)
		{
			this.text = text;
			collChunks = new LinkTreeNodeChunkCollection();
			under = new Rectangle[0];
			CalcText();
		}

		/// <summary>
		/// Create the object.
		/// </summary>
		/// <param name="text">The text for the node.</param>
		public LinkTreeNode(string text)
			: base(text)
		{
			this.text = text;
			collChunks = new LinkTreeNodeChunkCollection();
			under = new Rectangle[0];
			CalcText();
		}

		/// <summary>
		/// Create the object.
		/// </summary>
		public LinkTreeNode()
		{
			collChunks = new LinkTreeNodeChunkCollection();
		}

		/// <summary>
		/// The actual text for the node. The property Text has spaces added so the scrolling is set.
		/// </summary>
		public string NodeText
		{
			get
			{
				return text;
			}
			set
			{
				text = value;
				CalcText();
			}
		}

		/// <summary>
		/// Delete button at end of node is visible.
		/// </summary>
		public bool DeleteVisible
		{
			get { return deleteVisible; }
			set
			{
				deleteVisible = value;
				CalcText();
			}
		}

		/// <summary>
		/// Delete button at end of node is enabled.
		/// </summary>
		public bool DeleteEnabled
		{
			get { return deleteEnabled; }
			set { deleteEnabled = value; }
		}

		/// <summary>
		/// The chunk returned when clicking on the delete bitmap.
		/// </summary>
		public object DeleteChunk
		{
			get { return deleteChunk; }
			set { deleteChunk = value; }
		}

		private void CalcText()
		{
			int numSpaces = collChunks.Count * 2 + text.Length / 10;
			if (DeleteVisible)
				numSpaces += 4;
			Text = text + new string(' ', numSpaces);
		}

		/// <summary>
		/// Clear all chunks from the node.
		/// </summary>
		public void ClearChunks()
		{
			collChunks.Clear();
			CalcText();
		}

		/// <summary>
		/// The chunks in the node. This should be treated in a Read/Only mode.
		/// </summary>
		public LinkTreeNodeChunkCollection Chunks
		{
			get { return collChunks; }
		}

		/// <summary>
		/// Add a chunk to this node.
		/// </summary>
		/// <param name="chunk">The chunk to be added.</param>
		public void AddChunk(LinkTreeNodeChunk chunk)
		{
			for (int index = 0; index < collChunks.Count; index++)
			{
				// found the insert point
				if (chunk.Start < collChunks[index].Start)
				{
					Trap.trap();
					if (chunk.Start + chunk.Length >= collChunks[index].Start)
						throw new IndexOutOfRangeException("LinkTreeNodeChunk overlap");
					collChunks.Insert(index, chunk);
					return;
				}
				// make sure no overlap on end of this one & chunk
				if (collChunks[index].Start + collChunks[index].Length >= chunk.Start)
					throw new IndexOutOfRangeException("LinkTreeNodeChunk overlap");
			}

			collChunks.Add(chunk);
			CalcText();
		}
	}
}